package com.db.person.controller;

import com.db.person.bean.Dept;
import com.db.person.service.IDeptService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @RequestMapping("/dept/list")
    public String toList(PageModel pageModel , Model model, Integer pageIndex){

        if(pageIndex!=null){
            pageModel.setPageIndex(pageIndex);
        }

        //查询出所有的部门数据  并进行 分页功能
        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());
        List<Dept> all = deptService.getAll();
        PageInfo<Dept> pageInfo = new PageInfo<>(all);

        List<Dept> list = pageInfo.getList();

        pageModel.setRecordCount((int)pageInfo.getTotal());

        model.addAttribute("list",list);
        model.addAttribute("pageModel",pageModel);

        model.addAttribute("hre","dept/list");

        return "dept/list";
    }

    @RequestMapping("/dept/toadd")
    public String toAdd(){
        return "dept/add";
    }

    @RequestMapping("/dept/add")
    public String add(Dept dept){

        //调用service mapper 插入数据库
        deptService.insertOne(dept);
        return "redirect:/dept/list";
    }

    @RequestMapping("/dept/toedit")
    public String toEdit(Dept dept,Model model){

        model.addAttribute("dept",dept);

        return "dept/edit";
    }

    @RequestMapping("/dept/edit")
    public String edit(Dept dept){

        //根据id进行修改(修改另外两个字段)
        deptService.setSomeById(dept);
        return "redirect:/dept/list";
    }

}
